package com.daervin.api.controller;

import com.daervin.svc.IMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by daervin on 2021/4/1
 */
@Controller
public class IndexController {
    @Autowired
    private IMappingService mappingService;

    @RequestMapping(value = "/{key}", method = RequestMethod.GET)
    public void index(HttpServletResponse response, @PathVariable String key) {
        try {
            String url = mappingService.mapping(key);
            if (StringUtils.isEmpty(url)) {
                url = "http://www.readhub.top";
            }
            response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            response.setHeader("Location", url);
        } catch (Exception e) {

        }
    }
}
